
var list="",page=1,pagesize=6,length=0,total;
var colors=new Array();
colors["总部"]="f_orange";
colors["新房"]="f_red";
colors["租房"]="f_orange";
colors["二手房"]="f_green";
colors["商业地产"]="f_blue";

$().ready(function(){
    $("input[name=kw]").focus();
if(list){
    creatlist(list);
    page=1;
    $("#page1").text(page);
    $(".position").show();
    length=list.length;
    total=Math.ceil(length/pagesize);
    if(total>0){
        $("#page").show();
    }
    $("#total").text(total);
    $("#ms").html(list.length);
}

setmore("cs",6);
setmore("bm",4);

$('.amore').on("click",function(){
    $(this).hide().nextAll().show();
});

$('.slideup').on("click",function(){
    $(this).hide().prevUntil(".amore").hide();
    $(this).parent().find(".amore").show().removeClass("check");
});

$(".onetype").delegate("li:not(.amore,.slideup)","click",function(){
    if($(this).attr("class")=="check"){
        $(this).removeClass("check");
    }else{
        $(this).addClass("check").siblings().removeClass("check");
    }
    search();
});


$("#next").on("click",function(){
     $(".plist").empty();
     page=Math.min(++page,total);
     creatlist(list);
     if(page==total){
         $("#next").css("color","#ccc");
     }else{
         $("#next").css("color","");
     }
     $("#prev").css("color","");
     $("#page1").text(page);
});

$("#prev").on("click",function(){
    $(".plist").empty();
    page=Math.max(--page,1);
    creatlist(list);
    $("#next").css("color","");
    if(page==1){
        $("#prev").css("color","#ccc");
    }else{
        $("#prev").css("color","");
    }
    $("#page1").text(page);
});




$(document).delegate(".piao > ul",{
        "mouseover":function(){
            $(this).addClass("liover");
        },
        "mouseout":function() {
            $(this).removeClass("liover");
        }
});

$(".plist").delegate(".zg","click",function(){
    $(".search").val($(this).text());
    $("#left li[class=check]").removeAttr("class");
    search();
});

$(window).keydown(function(e){
    if(e.keyCode==13){
        search(1);
        return false;
    }
});


$("#loading").bind("ajaxSend",function(){
        $(this).show();
    }).bind("ajaxComplete",function(){
        $(this).hide();
    });
});


function setmore(id,num){
    length=$("#"+id+" li").length;
    $("#"+id+" li").each(function(i){
        if(i==num){
            $(this).after("<li class='amore'  ><a href='javascript:void(0)'>更多>></a></li>");
        }
        if(i>num){
            $(this).hide();
        }
        if((length-1)==i){
            $(this).after("<li class='slideup'  style='display:none;' ><a href='javascript:void(0)'>收起</a></li>");
        }
    });
}


function search(w){
    var test='';
        page=1;
    if($(".search").val()==""){
        alert("～忽悠我呢，先输入点东西～");
        return;
    }
    if(w){
        $("#search_form").submit();
        return ;
    }
    $(".plist").empty();

    $("#page1").text(page);
    $("#page").hide();
    $("#total").text("");
    $("#ms").text("");
    $(".position").show();
    $("#skw").html($(".search").val());

    $(".onetype").each(function(){
        if(!w && $(this).find("li.check").attr("sid"))
        test+="&"+$(this).attr("id")+"="+$(this).find("li.check").attr("sid");
    });

    params="ajax=1&kw="+encodeURI($(".search").val())+test;
    window.location.href=search_url+"?"+params;
    return ;
    $.ajax({
        url:search_url,
        data:params,
        dataType:"json",
        success:function(data){
            if(!data){
                //alert("没有找到符合条件的记录")
                $("#loading").hide();
                $("#ms").text(0);
                return false;
            }
            data.md5key==md5key?"":setleft(data);
            md5key=data.md5key;
            creatlist(data.rs);
            list=data.rs;
            length=data.rs.length;
            total=Math.ceil(length/pagesize);
            if(total>0)$("#page").show();
            $("#total").text(total);
            $("#ms").html(length);
        },
      error:function(){
          window.location.reload();
      }
    });
}

var md5key;
function setleft(data){
    $("#bm ul").empty() && $("#sy ul").empty() && $("#cs ul").empty();
    $.each(data.bm,function(k,v){
        $("#bm ul").append('<li sid="'+k+'"><a href="javascript:void(0)"  >'+v+'</a></li>');

    });
    $.each(data.sy,function(k,v){
        $("#sy ul").append('<li sid="'+k+'"><a href="javascript:void(0)"  >'+v+'</a></li>');

    });
    $.each(data.cs,function(k,v){
        $("#cs ul").append('<li sid="'+k+'"><a href="javascript:void(0)"  >'+v+'</a></li>');

    });
    $.each(["#bm li","#sy li","#cs li"],function(k,v){
        if($(v).length==1)
           $(v).addClass("check");

    });
}
function creatlist(data)
{
    $.each(data,function(k,v){
        if((page-1)*pagesize<=k&&k<page*pagesize){
            var is_game = game_list[v.user_code];
            var game_str = '<a href="'+active_url+'" target="_blank" title="2013绽放年"><img height="25" width="25" src="'+img_path+'1.png" style="padding-left:10px;padding-right:5px;"></a>';
            if(is_game){
                game_str +='<a href="'+one_hour_url+'" target="_blank"  title="环保达人"><img height="25" width="25" src="'+img_path+'2.png"></a>';
            }
            var html = '<li class="piao">'
            +'<img class="ig1" src="' + v.headpic + '"/>'
            + '<ul class="infolist">'
            + '<li class="name_indent '+getstatus(v.access_record)+'" >'
            + '<strong style="padding-left: 5px;">' + v.real_name + '</strong>'
            + '<span class="sp1">' + v.english_name + '</span>'
            + '<span >' + v.user_code + '</span>'
            + '</li>'
            + '<li class="li1">' + v.city_name + '<span class="bm1 '+getcolor(v.department_name)+'">'+v.department_name+'</span>' + '</li>'

            //+ '<li class="li2">'+v.position_name+(v.p_m_level!="&nbsp;"?'-':'')+v.p_m_level+'</li>'
            + '<li class="li2"><span style="vertical-align:top">'+v.position_name+'</span> '+game_str+'</li>'
            + '<li><a  class="email" href="mailto:'+v.office_mail+'">'+v.office_mail+'</a></li>'
            + '<li class="li1">'+v.office_phone+'</li>';
            if(v.superior_name != '' && v.superior_name != '&nbsp;'){
                html += '<li class="li2"><span class="sp2">'+v.cellphone+'</span> 上级主管：<a class="zg" >'+v.superior_name+'</a></li></ul></li>';
            }else{
                html += '<li class="li2"><span class="sp2">'+v.cellphone+'</span> 上级主管：</li></ul></li>';
            }
            $(".plist").append(html);
        };
    });
}
function getstatus(rid)
{
    if(rid==1)
        return "name_ico3";
    return "name_ico";
}

function getcolor(id)
{
    for(k in colors)
        {
        if(id.indexOf(k)>-1)
            return colors[k];
        }
    return 'f_orange';
}



